﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolLibrary.Entity;
using SchoolLibrary.Util;

namespace SchoolLibrary.DataBase.DAO
{
    public class RegistryJournalDAO : AbstractDAO<RegistryJournal>
    {
        private static RegistryJournalDAO instance;

        private RegistryJournalDAO() { }

        public static RegistryJournalDAO getInstance()
        {
            if (instance == null)
            {
                instance = new RegistryJournalDAO();
            }
            return instance;
        }

        public override List<RegistryJournal> findAll()
        {
            Query query = new Query(RegistryJournal.FIND_REGISTRY_JOURNAL_RECORDS);
            return getDAOHelper().getResultList(query);
        }

        public override RegistryJournal findById(long id)
        {
            throw new NotImplementedException();
        }

        public void updateReturnDate(long id, DateTime date)
        {
            Query query = new Query(RegistryJournal.UPDATE_RETURN_DATE);
            query.setParameter("date", DateUtil.formatDate(date, DateUtil.DD_MM_YYYY_DOTS_PATERN));
            query.setParameter("id", id);
            getDAOHelper().executeUpdate(query);
        }

        public override void save(RegistryJournal record)
        {
            Query query = new Query(RegistryJournal.INSERT_REGISTRY_JOURNAL_RECORD);
            query.setParameter("book_id", record.Book.Id);
            query.setParameter("pupil_id", record.Pupil.Id);
            query.setParameter("bail", record.Bail);
            query.setParameter("day_amount", record.DayAmount);
            query.setParameter("issue_date", DateUtil.formatDate(record.IssueDate, DateUtil.DD_MM_YYYY_DOTS_PATERN));
            getDAOHelper().executeUpdate(query);
        }

    }
}
